package com.reggie.controller.admin;

import com.reggie.dto.SetmealDTO;
import com.reggie.dto.SetmealPageQueryDTO;
import com.reggie.mapper.SetmealDishMapper;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.SetmealService;
import com.reggie.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关的接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐分页功能
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页接口")
    public R<PageResult> pageQuery(SetmealPageQueryDTO pageQueryDTO){
        log.info("套餐分页的参数：" + pageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(pageQueryDTO);
        return R.success(pageResult);
    }

    /**
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public R<SetmealVO> findById(@PathVariable Long id){
        log.info("根据id查询套餐的参数：" + id);
        SetmealVO setmealVO = setmealService.findById(id);
        return R.success(setmealVO);
    }

    /**
     * 套餐的起售和停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售和停售")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> startOrStop(@PathVariable Integer status,Long id){
        log.info("套餐起售和停售的参数：" + status,id);
        setmealService.startOrStop(status,id);
        return R.success();
    }

    /**
     * 新增套餐
     * @return
     */
    @PostMapping
    @ApiOperation("套餐的新增")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐的参数：" + setmealDTO);
        setmealService.sace(setmealDTO);
        return R.success();
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐接口参数:" + setmealDTO);
        setmealService.update(setmealDTO);
        return R.success();
    }

    /**
     *删除套餐
     */
    @DeleteMapping
    @ApiOperation("删除套餐接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐参数：" + ids );
        setmealService.deleteBatch(ids);
        return R.success();
    }
}
